﻿// See https://aka.ms/new-console-template for more information
using DVM.DB.DataQuery;
using DVM.DB.Model;
using DVM.DV.Extensions;
using DVM.VM;
using Newtonsoft.Json;

Console.WriteLine("**************分割线****************");

List<Product> dbQuery = DBData.GetProductQuery();
Console.WriteLine("DB结果:\r\n" + JsonConvert.SerializeObject(dbQuery, Formatting.Indented));
Console.WriteLine("**************分割线****************");


Console.WriteLine("**************分割线****************");
#region 重复性代码
var pdtIds = dbQuery.Select(x => x.ProductId).ToList();
var pdtDbQuery = ProductTypeQuery.QueryProductTypeFromDB(pdtIds);
var pdtDic = pdtDbQuery.Distinct().ToDictionary(x => x.Id, x => x.ProductTypeName);

var imageIds = dbQuery.Select(x => x.ImageID).ToList();
var imgDbQuery = ImageQuery.QueryImageViewFromDB(imageIds);
var imgDic = imgDbQuery.Distinct().ToDictionary(x => x.Id, x => new ImageViewModel
{
    Id = x.Id,
    ImageUrl = x.ImageUrl,
});
#endregion


var result1 = dbQuery.Select(x => new ProductViewModel
{
    ImageUrl = imgDic.ContainsKey(x.ImageID) ? imgDic[x.ImageID] : new ImageViewModel(),
    ProductTypeName = pdtDic.ContainsKey(x.ProductId) ? pdtDic[x.ProductId] : string.Empty,

    #region 可自动属性映射部分
    ProductId = x.ProductId,
    OtherId = x.OtherId,
    #endregion

    //自定义业务逻辑计算
    Calculate = x.ProductId + 1
});
Console.WriteLine("传统转化结果: \r\n" + JsonConvert.SerializeObject(result1, Formatting.Indented));
Console.WriteLine("**************分割线****************");


Console.WriteLine("**************分割线****************");
var result = dbQuery.AutoFieldMapper((x) => new ProductViewModel
{
    //自定义业务逻辑计算
    Calculate = x.ProductId + 1
});
Console.WriteLine("AutoField转化结果: \r\n" + JsonConvert.SerializeObject(result, Formatting.Indented));
Console.WriteLine("**************分割线****************");


/*
 解决问题：
1.减少重复性查询代码构建
2.面向属性赋值，让属性查询具有高复用性
3.内置属性自动映射
 */